#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: liang kang
@contact: gangkanli1219@gmail.com
@time: 2018/1/22 19:39
@desc: 生成mask矩阵，将待检测的鱼眼图像分为内外两环
       内环旋转5次，外环旋转12次，共得到17个buffer矩阵，
       对固定buffer矩阵的区域增加权值，然后所有buffer矩阵
       叠加，不为零的地方区倒数，既得mask矩阵。
"""
import numpy as np
from skimage import transform

shape = (1280, 1280)
mask = np.zeros(shape).astype(np.float32)

for angle in range(0, 180, 30):
    buffer = np.zeros(shape)
    buffer[40:360, 480:800] += 1
    buffer[920:1240, 480:800] += 1
    arc = angle * np.pi / 180
    post_size = (np.ceil(abs(shape[0] * np.cos(arc)) + abs(shape[1] * np.sin(arc))).astype(np.int32),
                 np.ceil(abs(shape[0] * np.sin(arc)) + abs(shape[1] * np.cos(arc))).astype(np.int32))
    buffer = transform.rotate(buffer, angle, True)
    d = list(map(lambda x, y: (x - y) // 2, post_size, shape))
    h, w = shape
    buffer = buffer[d[0]:(d[0] + h), d[1]:(d[1] + w)]
    buffer = np.where(buffer > 0, 1, buffer)
    mask += buffer

for angle in range(0, 360, 72):
    buffer = np.zeros(shape)
    buffer[320:640, 480:800] += 1
    arc = angle * np.pi / 180
    post_size = (np.ceil(abs(shape[0] * np.cos(arc)) + abs(shape[1] * np.sin(arc))).astype(np.int32),
                 np.ceil(abs(shape[0] * np.sin(arc)) + abs(shape[1] * np.cos(arc))).astype(np.int32))
    buffer = transform.rotate(buffer, angle, True)
    d = list(map(lambda x, y: (x - y) // 2, post_size, shape))
    h, w = shape
    buffer = buffer[d[0]:(d[0] + h), d[1]:(d[1] + w)]
    buffer = np.where(buffer > 0, 1, buffer)
    mask += buffer

with np.errstate(divide='ignore'):
    mask = np.where(mask > 0, 1.0 / mask, 0)
np.save('./mask.npy', mask)

